import { UI_Listen, UI_Layer } from '../../config/ui_def'
import { WX_AD_CONFIG, Sdk_Platform, VIVO_AD_CONFIG, OPPO_AD_CONFIG } from '../../frame/constants';
import { getGachaid } from '../tool/gacha';

cc.Class({
    extends: cc.Component,

    properties: {
        img_sp: cc.Sprite,
        icon: cc.Sprite,
        title: cc.Label,
        dec_lab: cc.Label,
        chayeNode: cc.Node,
        bannerNode: cc.Node,
        bannerPic: cc.Sprite,
        bottomBanner: cc.Node,
        bottomWidge: cc.Widget,
        bottomSecWidge: cc.Widget,
        bannerIcon: cc.Sprite,
        bannerTitle: cc.Label,
        bannerDes: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {
        // he.ad.
    },

    updatePage(param) {
        this.chayeNode.active = false
        this.bannerNode.active = false
        this.showPtBanner = false
        this.isShowAd = false
        // he.ad.closeBannerOnload()
        let isShowAd = param.isShowAd
        this.isShowBanner = param.isShowBanner
        if (param.pageName) {
            this.pageName = param.pageName
            switch (this.pageName) {
                case 'signPage':
                    this.bottomWidge.bottom = 0
                    this.bottomWidge.updateAlignment()
                    this.bottomSecWidge.bottom = 0
                    this.bottomSecWidge.updateAlignment()
                    break
                case 'GameOver':
                    this.bottomWidge.bottom = 0
                    this.bottomWidge.updateAlignment()
                    this.bottomSecWidge.bottom = 0
                    this.bottomSecWidge.updateAlignment()
                    break
                default:
                    this.bottomWidge.bottom = 100
                    this.bottomWidge.updateAlignment()
                    this.bottomSecWidge.bottom = 150
                    this.bottomSecWidge.updateAlignment()
                    break
            }
        }
        this.show = false
        this.adSwitch(isShowAd, this.isShowBanner)
    },

    closeUI() {
        gg.eventManager.emit('main', { type: UI_Listen.MAIN_ENDAD })
        // gg.eventManager.emit('playground', { type: UI_Listen.PLGD_ENDAD })
        switch (common.sdk_env) {
            case Sdk_Platform.VIVO:
                if (this.showPtBanner) {
                    this.bottomBanner.name = VIVO_AD_CONFIG.bannerId[0]
                    he.ad.changeBanner(false, this.bottomBanner)
                    he.ad.closeBannerOnload()
                }
                break
        }
        this.bannerNode.active = false
        if (this.schedLoad) {
            this.unschedule(this.schedLoad)
            this.schedLoad = null
        }
    },

    closeServer() {
        this.chayeNode.active = false
        this.bannerNode.active = false
        this.uiBase.closePage()
    },

    adSwitch(status, isShowBanner) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    break
                case Sdk_Platform.VIVO:
                    this.ads
                    if (!isShowBanner) {
                        //茶叶
                        // this.ads = he.ad.getNativeJson(VIVO_AD_CONFIG.nativeAd[0])
                        // this.ads = qg.createNativeAd({
                        //     posId: VIVO_AD_CONFIG.nativeAd[1],
                        // });
                        this.ads = he.ad.getNativeJson(VIVO_AD_CONFIG.nativeAd[1], this.successServer.bind(this), this.failServer.bind(this))
                    } else {
                        //banner
                        // this.ads = he.ad.getNativeJson(VIVO_AD_CONFIG.nativeAd[0])
                        // this.ads = qg.createNativeAd({
                        //     posId: VIVO_AD_CONFIG.nativeAd[0],
                        // });
                        this.ads = he.ad.getNativeJson(VIVO_AD_CONFIG.nativeAd[0], this.successServer.bind(this), this.failServer.bind(this))
                    }
                    if (!this.ads) {
                        this.failServer()
                        return
                    }

                    if (this.ads === true) {
                        break
                    }
                    if (this.ads) {
                        this.successServer(this.ads)
                    }

                    break
                case Sdk_Platform.OPPO:
                    break
            }
        } else {
            //  关掉广告
        }
    },

    // update(dt) {
    //     console.log(this.show)
    // },


    successServer(res) {
        // console.log(res, '成功的回调')
        this.show = true
        if (res == [] || res == null || res == undefined) {
            this.failServer()
            return
        }
        this.bottomBanner.active = false
        if (!this.isShowBanner) {
            this.chayeNode.active = true
            this.bannerNode.active = false
            he.local.click(`vivo_ys_chaye_${this.pageName}`)
            if (res)
                this.vivoAdInit(res)
        } else {
            this.chayeNode.active = false
            this.bannerNode.active = true
            he.local.click(`vivo_ys_banner_${this.pageName}`)
            if (res)
                this.vivoBannerInit(res)
        }
    },

    failServer() {
        this.chayeNode.active = false
        this.bannerNode.active = false
        this.bottomBanner.active = true
        if (!this.isShowBanner) {
            he.local.click(`vivo_pt_chaye_${this.pageName}`)
            he.ad.interstitialShow(this.closeServer.bind(this))
        } else {
            this.showPtBanner = true
            he.local.click(`vivo_pt_banner_${this.pageName}`)
            this.bottomBanner.name = VIVO_AD_CONFIG.bannerId[0]
            he.ad.changeBanner(true, this.bottomBanner, this.closeServer.bind(this))

        }

    },


    vivoBannerInit(info) {
        let adInfo = info
        if (adInfo) {
            this.loadImg(this.bannerIcon, adInfo.imgUrlList[0], 102, 102)
            let desc = adInfo.desc || '这真是太好了'
            let title = adInfo.title || '精选'
            this.bannerDes.string = `${desc}`
            this.bannerTitle.string = `${title}`
        } else {
            this.chayeNode.active = false
            this.bannerNode.active = false
            this.bottomBanner.active = true
            this.showPtBanner = true
            he.local.click(`vivo_pt_banner_${this.pageName}`)
            this.bottomBanner.name = VIVO_AD_CONFIG.bannerId[0]
            he.ad.changeBanner(true, this.bottomBanner, this.closeServer.bind(this))
        }
    },

    scheduleLoad() {
        this.schedLoad = this.schedule(0.5, () => {
            if (this.ads && !this.isShow) {
                this.ads.load()
            }
        })
    },



    ClickServer() {
        let isshow
        if (this.isShowBanner) {
            isshow = 'banner'
            he.ad.clickNative(VIVO_AD_CONFIG.nativeAd[0])
        } else {
            isshow = 'chaye'
            he.ad.clickNative(VIVO_AD_CONFIG.nativeAd[1])
        }
        he.local.click(`vivo_ys_${isshow}_${this.pageName}`)
        this.closeServer()
    },

    vivoAdInit(info) {
        let adInfo = info
        if (adInfo) {
            this.loadImg(this.img_sp, adInfo.imgUrlList[0], 550, 400)
            if (adInfo.icon && adInfo.length >= 4) {
                this.loadImg(this.icon, adInfo.icon, 102, 102)
                this.icon.node.active = true
            } else {
                this.icon.node.active = false
            }
            let desc = adInfo.desc || '这真是太好了'
            let title = adInfo.title || '精选'
            this.title.string = `${title}`
            this.dec_lab.string = `${desc}`
            // this.priceLab.string = `${adInfo.title}`
        } else {
        }
    },
    loadImg(container, url, width, height) {
        if (url.length < 4) {
            return he.error(("图片长度不对错误"));
        }
        let ext = url.substring(url.length - 4, url.length)
        ext = ext.toLowerCase()
        if (ext == 'jpeg') ext = '.jpg'
        cc.assetManager.loadRemote(url, { ext: ext }, (err, texture) => {
            if (err) {
                console.log(err)
                he.error(err)
            }
            if (this.callFun) {
                this.callFun()
            }
            var sprite = new cc.SpriteFrame(texture);
            container.spriteFrame = sprite;
            container.node.setContentSize(width, height)
            // this.icon.node.setContentSize(240, 240)
        });
    },
});
